import { login } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { setAuthority, removeAuthority } from '@/utils/permission'

const getDefaultState = () => {
    return {
        token: getToken(),
        name: getToken(),
        avatar: ''
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    }
}

const actions = {
    // user login
    login({ commit }, userInfo) {
        const { username, password, captchaCode } = userInfo
        return new Promise((resolve, reject) => {
            login({ username: username.trim(), password: password, captchaCode })
                .then((response) => {
                    const { data, code } = response

                    if (code === 200) {
                        console.log('data---->', data)
                        setToken(JSON.stringify(data))
                        commit('SET_TOKEN', data)
                        commit('SET_NAME', data)
                        resolve()
                    } else {
                        console.log('错误了---->', code)
                        reject(error)
                    }
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    // get user info
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            const token = getToken()
            getInfo({ token })
                .then((response) => {
                    const { data } = response

                    if (!data) {
                        return reject('Verification failed, please Login again.')
                    }

                    const { name, avatar, roles } = data

                    commit('SET_NAME', name)
                    commit('SET_AVATAR', avatar)
                    setAuthority(roles)
                    resolve(data)
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },
    // user logout
    logout({ commit, dispatch, state }) {
        return new Promise((resolve, reject) => {
            sessionStorage.setItem('hasLogin', 1)
            removeToken() // must remove  token  first
            removeAuthority()
            resetRouter()
            commit('permission/SET_ROUTES', [], { root: true })
            commit('RESET_STATE')
            resolve()
        })
    },

    // remove token
    resetToken({ commit }) {
        return new Promise((resolve) => {
            removeToken() // must remove  token  first
            commit('RESET_STATE')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
